package spbun.fileclean.common;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

import spbun.fileclean.dto.FileInfo;

public class FileTravel {

	private File forder = null;

	public boolean finish = false;

	public List<FileInfo> list = new ArrayList<>();

	// private long mixSize = 1024 * 1024 * 20;

	public FileTravel(File forder) {
		this.forder = forder;
	}

	public void travel() throws IOException {

		travel(forder, list);
	}

	private void travel(File file, List<FileInfo> list) throws IOException {

		Path path = Paths.get(file.getAbsolutePath());

		SimpleFileVisitor<Path> finder = new SimpleFileVisitor<Path>() {
			@Override
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

				File tempFile = file.toFile();

				// if (tempFile.length() > mixSize) {
				FileInfo obj = new FileInfo(tempFile.getName(), tempFile.length(), tempFile.getAbsolutePath());
				list.add(obj);
				// }

				return super.visitFile(file, attrs);
			}

			@Override
			public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {

				System.out.println("处理文件失败：" + file.toFile());

				return FileVisitResult.CONTINUE;
			}
		};

		java.nio.file.Files.walkFileTree(path, finder);

		finish = true;
	}
}
